<template>
  <el-form ref="form" :model="listQuery">
    <el-form-item label="类别：">
      <category-tree
        ref="categoryTree"
        :DefaultClear="true"
        @changeData="
          (res) => {
            listQuery.categorySn = res.categortSn;
            submit();
          }
        "
      ></category-tree>
    </el-form-item>
    <el-form-item>
      <el-input
        placeholder="请输入商品名称"
        style="width: 200px"
        v-model="listQuery.params"
      ></el-input>
      <el-button type="primary" style="margin-left: 10px" @click="getPage()"
        >查询</el-button
      >
    </el-form-item>
  </el-form>

  <div>
    <el-button type="primary" style="margin-bottom: 10px" @click="add()"
      >添加</el-button
    >
  </div>
  <el-table :data="tableData" border style="width: 100%">
    <el-table-column type="index" label="序号" width="180" align="center">
    </el-table-column>
    <el-table-column
      prop="productName"
      label="商品名称"
      width="180"
      align="center"
    >
    </el-table-column>
    <el-table-column
      prop="categoryName"
      label="类别"
      width="180"
      align="center"
    >
    </el-table-column>
    <el-table-column
      prop="price"
      label="价格"
      width="180"
      align="center"
    ></el-table-column>
    <el-table-column
      prop="stock"
      label="库存"
      width="180"
      align="center"
    ></el-table-column>
    <el-table-column label="操作" align="center">
      <template #default="scope">
        <el-button @click="update(scope.row)" type="primary">修改</el-button>
        <el-button @click.prevent="deleteRow(scope.$index, tableData)" type="danger">移除</el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-pagination
    style="float: right"
    background
    layout="total,prev, pager, next"
    @current-change="getPage"
    :total="listQuery.total"
  >
  </el-pagination>
  <!-- 添加/修改 -->
  <el-dialog
    title="添加/修改"
    width="500px"
    v-if="addOrUpdateVisible"
    v-model="addOrUpdateVisible"
  >
    <el-form :model="addForm" label-width="90px" :rules="addRules">
      <el-form-item label="类别：" prop="categorySn">
        <category-tree
        ref="addCategoryTree"
          :DefaultParentData="categories"
          :DefaultData="addForm.categorySn"
          @changeData="
            (res) => {
              addForm.categorySn = res.categortSn;
            }
          "
        ></category-tree>
      </el-form-item>
      <el-form-item label="商品名称：" prop="productName">
        <el-input v-model="addForm.productName" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="商品图片：">
        <el-upload
          action="https://jsonplaceholder.typicode.com/posts/"
          list-type="picture-card"
          :multiple="true"
          :limit="3"
          :on-preview="handlePictureCardPreview"
          :on-remove="handleRemove"
          :file-list="addForm.imgs"
        >
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog v-model="dialogVisible">
          <el-image
            style="width: 500px; height: 500px"
            fit="fill"
            :src="dialogImageUrl"
            alt=""
          />
        </el-dialog>
      </el-form-item>
      <el-form-item label="价格：" prop="price">
        <el-input v-model="addForm.price" />
      </el-form-item>
      <el-form-item label="库存：" prop="stock">
        <el-input v-model="addForm.stock" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="addOrUpdateVisible = false">取 消</el-button>
        <el-button type="primary" @click="addOrUpdateVisible = false"
          >确 定</el-button
        >
      </span>
    </template>
  </el-dialog>
</template>

<script>
import axios from "axios";
import CategoryTree from "../../components/CategoryTree.vue";
export default {
  components: {
    CategoryTree,
  },
  data() {
    var checkPrice = (rule, value, callback) => {
      if (value == undefined || "" === value) {
        return callback(new Error("价格不能为空"));
      }
      var pattern = /^(?:0\.\d{0,1}[1-9]|(?!0)\d{1,6}(?:\.\d{0,1}[1-9])?)$/;
      if (pattern.test(value)) {
        callback();
      } else {
        callback(new Error("请输入正确的数字值"));
      }
    };
    var checkStock = (rule, value, callback) => {
      if (value == undefined || "" === value) {
        return callback(new Error("库存不能为空"));
      }
      var pattern = /^(0|[1-9][0-9]*)$/;
      if (pattern.test(value)) {
        callback();
      } else {
        callback(new Error("请输入数字值"));
      }
    };
    return {
      listQuery: {
        params: "",
        categorySn: "",
        current: 1,
        total: 100,
        size: 10,
      },
      addOrUpdateVisible: false,
      addForm: {},
      dialogImageUrl: "",
      dialogVisible: false,
      tableData: [],
      categories: [],
      addRules: {
        productName: [
          { required: true, trigger: "blur", message: "请输入商品名称" },
        ],
        price: [
          {
            required: true,
            validator: checkPrice,
            trigger: "blur",
          },
        ],
        stock: [
          {
            required: true,
            validator: checkStock,
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getPage();
  },
  methods: {
    getPage() {
      axios
        .post("http://localhost:8090/products", this.listQuery)
        .then((res) => {
          this.tableData = res.data.data.products;
          this.listQuery.total = res.data.data.total;
          //获取所有的类别
          this.categories = this.$treeData(
            res.data.data.categories,
            "categorySn",
            "parentCategorySn",
            "children"
          );
          setTimeout(() => {
            this.$refs.categoryTree.parentData = this.categories;
          }, 200);
        });
    },
    submit() {
      this.listQuery.current = 0;
      this.getPage();
    },
    update(row) {
      console.log(row);
      (this.title = "修改${row.userName}"), (this.addForm = row);
      this.addOrUpdateVisible = true;
    },
    deleteRow(index, rows) {
        rows.splice(index, 1);
      },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
  },
};
</script>